package com.bw.dwd;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bw.MyKafkaUtil;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.core.fs.Path;
import org.apache.flink.runtime.state.filesystem.FsStateBackend;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.CheckpointConfig;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

public class Test2 {

    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env=StreamExecutionEnvironment.createLocalEnvironment();
        env.setParallelism(1);

        DataStream<String> stream = env.addSource(MyKafkaUtil.getKafkaConsumer("ods_base_topic_yk8", "test" + System.currentTimeMillis()));



        OutputTag<String> orderInfoTag = new OutputTag<String>("order_info", TypeInformation.of(String.class));
        OutputTag<String> orderDetailTag = new OutputTag<String>("order_detail", TypeInformation.of(String.class));

        SingleOutputStreamOperator<String> splitStream = stream.process(new ProcessFunction<String, String>() {
            @Override
            public void processElement(String s, ProcessFunction<String, String>.Context context, Collector<String> collector) throws Exception {
                JSONObject jsonObject = JSON.parseObject(s);
                String table = jsonObject.getString("table");
                if ("order_info".equals(table)) {
                    context.output(orderInfoTag, jsonObject.toJSONString());
                }
                if ("order_detail".equals(table)) {
                    context.output(orderDetailTag, jsonObject.toJSONString());
                }

            }
        });

        // 取流
        DataStream<String> orderInfo = splitStream.getSideOutput(orderInfoTag);
        DataStream<String> orderDetail = splitStream.getSideOutput(orderDetailTag);


        orderInfo.print("orderInfo>>");
        orderDetail.print("orderDetail>>");
        orderInfo.addSink(MyKafkaUtil.getKafkaProducer("dwd_order_info_topic_yk8"));
        orderDetail.addSink(MyKafkaUtil.getKafkaProducer("dwd_order_detail_topic_yk8"));

        env.execute();
    }
}
